package com.server.rule.component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;



import com.server.rule.data.Data;
import com.server.rule.data.ExceptionTypeData;
import com.server.rule.component.integrated.RuleDecisionFactory;
import com.server.rule.component.triple.Triple;
import com.server.rule.component.triple.complex.AndTriple;
import com.server.rule.component.triple.complex.ComplementTriple;
import com.server.rule.component.triple.complex.OrTriple;
import com.server.rule.component.triple.simple.BooleanTriple;
import com.server.rule.component.triple.simple.NominalTriple;
import com.server.rule.component.triple.simple.NumericTriple;
import com.shared.rule.TypeDecision;

/**
 * Classe représentant un noeud de l'arbre
 * @author queyroi
 *
 */
public class RuleComposite extends AbstractRuleComposite{
	/**
	 * Condition associé au noeud
	 */
	private Triple triple;
		
	public RuleComposite() {
		triple = null;
		nextRules = new ArrayList<RuleComponent>();
	}
	
	/**
	 * @return the triple
	 */
	public Triple getTriple() {
		return triple;
	}

	/**
	 * @param triple the triple to set
	 */
	public void setTriple(Triple triple) {
		this.triple = triple;
	}

	

	@Override
	public void operate(Data data,DecisionHandler handler) throws ExceptionTypeData {	
			if(triple.match(data)){
				for(RuleComponent r : nextRules){
					r.operate(data,handler);
				}
			}		
	}
	
	
	
	@Override
	public Element getXMLElement() {
		Element xmlElement = new Element("Rule");
		xmlElement.setAttribute("type", "Composite");
		xmlElement.setAttribute("label", triple.getHumanReadable());
		xmlElement.addContent(triple.getXMLElement());
		for(RuleComponent r : nextRules){
			xmlElement.addContent(r.getXMLElement());
		}
		return xmlElement;
	}

	@Override
	public void generateFromXML(Element xmlElement) {	
		label = xmlElement.getAttributeValue("label");
		
			if(xmlElement.getChild("Triple").getAttributeValue("type").equals("NominalTriple")) triple = new NominalTriple();
			if(xmlElement.getChild("Triple").getAttributeValue("type").equals("NumericTriple")) triple = new NumericTriple();
			if(xmlElement.getChild("Triple").getAttributeValue("type").equals("BooleanTriple")) triple = new BooleanTriple();
			if(xmlElement.getChild("Triple").getAttributeValue("type").equals("AndTriple")) triple = new AndTriple();
			if(xmlElement.getChild("Triple").getAttributeValue("type").equals("OrTriple")) triple = new OrTriple();
			if(xmlElement.getChild("Triple").getAttributeValue("type").equals("ComplementTriple")) triple = new ComplementTriple();
			triple.generateFromXML(xmlElement.getChild("Triple"));
		
		
		Iterator iteratorElement = (xmlElement.getChildren("Rule")).iterator();
		while(iteratorElement.hasNext()){
			RuleComponent ruleTemp = null;
			Element elementTemp = (Element) iteratorElement.next();
			if(elementTemp.getAttributeValue("type").equals("Composite")){
				ruleTemp=new RuleComposite();
			}else{
				String type = elementTemp.getAttributeValue("type");
				ruleTemp = RuleDecisionFactory.get().createDecision(TypeDecision.getTypeDecision(type));
			}
			ruleTemp.generateFromXML(elementTemp);
			add(ruleTemp);			
		}
		
		
	}

	@Override
	public void getUsedVar(List<String> listeUsedVar) {
		triple.getUsedVar(listeUsedVar);	
		for(RuleComponent r : nextRules) r.getUsedVar(listeUsedVar);		
	}
	

	
	

}
